package com.suredy.app.porject.ctrl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suredy.app.porject.constants.WorkPlanMilestone;
import com.suredy.app.porject.ctrl.flow.FlowCtrl;
import com.suredy.app.porject.model.vo.PayinPlanFilterModel;
import com.suredy.app.porject.model.vo.PayoutPlanFilterModel;
import com.suredy.app.porject.model.vo.WorkPlanFilterModel;
import com.suredy.app.porject.roles.SystemRoles;
import com.suredy.app.porject.service.ContractPayinPlanSrv;
import com.suredy.app.porject.service.ContractPayoutPlanSrv;
import com.suredy.app.porject.service.ContractWorkPlanSrv;
import com.suredy.security.service.RoleSrv;
import com.suredy.security.service.User2RoleSrv;

/**
 * 收款计划管理
 * 
 * @author VIVID.G
 * @since 2015-12-28
 * @version v0.1
 */
@Controller
@RequestMapping("/contract-plan")
public class ContractPlanCtrl extends FlowCtrl {

	@Autowired
	protected RoleSrv roleSrv;

	@Autowired
	protected User2RoleSrv user2RoleSrv;

	@Autowired
	private ContractPayinPlanSrv contractPayinPlanSrv;

	@Autowired
	private ContractPayoutPlanSrv contractPayoutPlanSrv;

	@Autowired
	private ContractWorkPlanSrv contractWorkPlanSrv;

	@RequestMapping(value = "/all-payin-plan-list")
	public String allPayinPlanList(PayinPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/all-payin-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new PayinPlanFilterModel();

		model.addAttribute("search", search);
		model.addAttribute("header", this.getUserByRoles(SystemRoles.r_people_money_in));
		model.addAttribute("data", this.contractPayinPlanSrv.getAllPlanList(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractPayinPlanSrv.getAllPlanListCount(search));

		return view;
	}

	@RequestMapping(value = "/my-payin-plan-list")
	public String myPayinPlanList(PayinPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/my-payin-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new PayinPlanFilterModel();
		search.setPlanHeader(this.getUser().getId());

		model.addAttribute("search", search);
		model.addAttribute("data", this.contractPayinPlanSrv.getAllAlarmPlanListOf(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractPayinPlanSrv.getAllAlarmPlanListCountOf(search));

		return view;
	}

	@RequestMapping(value = "/all-payout-plan-list")
	public String allPayoutPlanList(PayoutPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/all-payout-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new PayoutPlanFilterModel();

		model.addAttribute("search", search);
		model.addAttribute("header", this.getUserByRoles(SystemRoles.r_people_money_out, SystemRoles.r_zhuanze_caigou, SystemRoles.r_zhuanze_tongji));
		model.addAttribute("data", this.contractPayoutPlanSrv.getAllPlanList(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractPayoutPlanSrv.getAllPlanListCount(search));

		return view;
	}

	@RequestMapping(value = "/my-payout-plan-list")
	public String myPayoutPlanList(PayoutPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/my-payout-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new PayoutPlanFilterModel();
		search.setPlanHeader(this.getUser().getId());

		model.addAttribute("search", search);
		model.addAttribute("data", this.contractPayoutPlanSrv.getAllAlarmPlanListOf(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractPayoutPlanSrv.getAllAlarmPlanListCountOf(search));

		return view;
	}

	@RequestMapping(value = "/all-work-plan-list")
	public String allWorkPlanList(WorkPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/all-work-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new WorkPlanFilterModel();

		model.addAttribute("search", search);
		model.addAttribute("milestone", WorkPlanMilestone.values());
		model.addAttribute("data", this.contractWorkPlanSrv.getAllPlanList(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractWorkPlanSrv.getAllPlanListCount(search));

		return view;
	}

	@RequestMapping(value = "/my-work-plan-list")
	public String myWorkPlanList(WorkPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/my-work-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new WorkPlanFilterModel();
		search.setPlanCreator(this.getUser().getId());

		model.addAttribute("search", search);
		model.addAttribute("milestone", WorkPlanMilestone.values());
		model.addAttribute("data", this.contractWorkPlanSrv.getAllPlanList(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractWorkPlanSrv.getAllPlanListCount(search));

		return view;
	}

}
